﻿@inherits AdminCompontentBase

<AutoHeight Card CardClass="pl-6 pr-2 py-0">
    <HeaderContent>
        <div class="d-flex">
            <MSpacer />
            <SSearch @bind-Value="Search" BackgroundColor="fff" />
            <div @onclick="() => SyncStaffDialogVisible=true" class="ml-4 d-flex button white" style="width: 70px;height:40px;">
                <div class="ma-auto">
                    <SIcon Color="emphasis2" Size=20 IsDefaultToolTip="false">@IconConstants.Sync</SIcon>
                    <span class="caption emphasis2--text">@T("Sync")</span>
                </div>
            </div>
            <SButton Small Class="ml-4" OnClick=OpenAddStaffDialog BorderRadiusClass="rounded-lg">
                <SIcon Size=18 IsDefaultToolTip="false">@IconConstants.Add</SIcon>
                <span class="ml-2 btn">@T("Add")</span>
            </SButton>
        </div>
    </HeaderContent>
    <AutoHeightContent>
        <SDataTable Headers="_headers"
		    Height="@("calc(100vh - 260px)")"
		    FixedHeader
		    Items="Staffs"
		    TItem="StaffDto"
		    ItemsPerPage="PageSize"
		    Class="mr-2">
            <HeaderColContent Context="header">
                <span>@header.Text</span>
            </HeaderColContent>
            <ItemColContent>
                @switch (context.Header.Value)
                {
                    case nameof(UserDto.Avatar):
                        <div class="hover-pointer d-flex" @onclick="()=> OpenUpdateStaffDialog(context.Item)">
                            <MAvatar Size=40>
                                <img src="@context.Item.Avatar" />
                            </MAvatar>
                            <div class="ml-2 my-auto">
                                <div>@ReturnNotNullValue(context.Item.DisplayName,context.Item.Name)</div>
                                <div class="regular2--text caption">@context.Item.PhoneNumber</div>
                            </div>
                        </div>
                        break;
                    case (nameof(StaffDto.Account)):
                        <span>@context.Item.Account</span>
                        break;
                    case (nameof(StaffDto.Department)):
                        <span>@(string.IsNullOrEmpty(context.Item.Department) ? "-" : context.Item.Department)</span>
                        break;
                    case nameof(StaffDto.JobNumber):
                        <span>@context.Item.JobNumber</span>
                        break;
                    case (nameof(StaffDto.Position)):
                        <span>@(string.IsNullOrEmpty(context.Item.Position) ? "-" : context.Item.Position)</span>
                        break;
                    case nameof(StaffDto.Enabled):
                        <EnableChip Value="context.Item.Enabled" DisabledLabel="@T("Freeze")" />
                        break;
                    case "Action":
                        <div class="d-flex">
                            <MButton Class="mr-4" Icon OnClick="()=> OpenUpdateStaffDialog(context.Item)">
                                <SIcon Tooltip="@T("Edit")">@IconConstants.Update</SIcon>
                            </MButton>
                            <MButton Class="mr-4" Icon OnClick="async ()=> await _userAuthorizeDialog.ShowAsync(context.Item.UserId)">
                                <SIcon Tooltip="@T("Authorize")">@IconConstants.Authorize</SIcon>
                            </MButton>
                        </div>
                        break;
                    default:
                        <span class="subtitle">@context.Value</span>
                        break;
                }
            </ItemColContent>
        </SDataTable>
    </AutoHeightContent>
    <FooterContent>
        <SPagination Class="mt-5" @bind-Page="Page" @bind-PageSize=PageSize Total=Total />
    </FooterContent>
</AutoHeight>

<AddStaffDialog @bind-Visible=AddStaffDialogVisible OnSubmitSuccess=UpdateUserAsync />
<UpdateStaffDialog @bind-Visible=UpdateStaffDialogVisible StaffId=CurrentStaff.Id OnSubmitSuccess=GetStaffAsync />
<UserAuthorizeDialog @ref="_userAuthorizeDialog" OnSubmitSuccess=GetStaffAsync />
<SyncStaffDialog @bind-Visible=SyncStaffDialogVisible OnSubmitSuccess=UpdateUserAsync />
